package com.gxa.shop.dao.carousel.impl;

import com.gxa.shop.bean.dto.Page;
import com.gxa.shop.bean.pojo.Carousel;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.dao.carousel.CarouselDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * com.gxa.shop.dao.carousel.impl
 * User: 刁彦侨
 * Date: 2024/12/19 10:17
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
public class CarouselDaoImpl implements CarouselDao {

    @Override
    public List<Carousel> getCarousel() {
        String sql = "select * from carousel";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString());
        if(query.size()>0){
            List<Carousel> carousels = new ArrayList<>();
            for (Map<String, Object> row : query) {
                Carousel carousel=new Carousel();
                carousel.setCarouselId((Integer) row.get("carousel_id") );
                carousel.setCarouselName((String) row.get("carousel_name"));
                carousel.setCarouselUrl((String) row.get("carousel_url"));
                carousel.setCarouselDescription((String) row.get("carousel_description"));
                carousels.add(carousel);
            }
            return  carousels;
        }
        return null;
    }

    @Override
    public int addCarousel(Carousel carousel) {
        String sql = "INSERT INTO carousel(carousel_name, carousel_url, carousel_description) VALUES (?, ?, ?)";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Object>  parameters=new ArrayList<>();
        parameters.add(carousel.getCarouselName());
        parameters.add(carousel.getCarouselUrl());
        parameters.add(carousel.getCarouselDescription());
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        return jdbcUtil.update(sql,parameters.toArray());
    }

    @Override
    public int update(Carousel carousel) {
        String sql = "update carousel set carousel_name=?, carousel_url=?, carousel_description=? where carousel_id=?";
        JDBCUtil jdbcUtil = new JDBCUtil();

        List<Object>  parameters=new ArrayList<>();
        parameters.add(carousel.getCarouselName());
        parameters.add(carousel.getCarouselUrl());
        parameters.add(carousel.getCarouselDescription());
        parameters.add(carousel.getCarouselId());

        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        int update = jdbcUtil.update(sql,parameters.toArray());
        return update;
    }

    @Override
    public int deleteById(Integer carouselId) {
        String sql = "delete from carousel where carousel_id=?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        int delete = jdbcUtil.update(sql,carouselId);
        return delete;
    }

    @Override
    public Long findByTotal() {
        String sql = "SELECT count(*) ct FROM carousel";
        JDBCUtil  jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString());
        //默认0条
        Long  ct=0L;
        if(query.size()>0){
            //该变
            ct=(Long)query.get(0).get("ct");
        }
        return ct;
    }

    @Override
    public List<Carousel> findByPage(Page page) {
        //sql
        StringBuffer sql=new StringBuffer("select  *  from  carousel  where  1=1 ");
        sql.append("  order by carousel_id desc ");
        sql.append("  limit  "+((page.getPage()-1)*page.getLimit()) +","+page.getLimit());
        System.out.println("sql:"+sql);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString());
        if(query.size()>0){
            List<Carousel> carousels = new ArrayList<>();
            for (Map<String, Object> row : query) {
                Carousel carousel=new Carousel();
                carousel.setCarouselId((Integer) row.get("carousel_id") );
                carousel.setCarouselName((String) row.get("carousel_name"));
                carousel.setCarouselUrl((String) row.get("carousel_url"));
                carousel.setCarouselDescription((String) row.get("carousel_description"));
                carousels.add(carousel);
            }
            return  carousels;
        }
        return null;
    }
}
